﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Showroom.DAL;

namespace Showroom.BLL
{
    public class ProductImages : _ProductImages
    {
        public static void DeleteAllImageByProductID(int productID)
        {
            try
            {
                ProductImages imgs = new ProductImages();
                imgs.Where.ProductID.Value = productID;
                imgs.Query.Load();
                do
                {
                    imgs.MarkAsDeleted();
                }
                while (imgs.MoveNext());
                imgs.Save();
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        public static void DeleteAllImage()
        {
            try
            {
                ProductImages imgs = new ProductImages();
                imgs.LoadAll();
                imgs.DeleteAll();
                imgs.Save();
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        public static ProductImages GetByProductID(int productID)
        {
            try
            {
                ProductImages imgs = new ProductImages();
                imgs.Where.ProductID.Value = productID;
                imgs.Query.Load();
                return imgs;
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        public static void DeleteByID(int ID)
        {
            try
            {
                ProductImages img = new ProductImages();
                img.LoadByPrimaryKey(ID);
                img.MarkAsDeleted();
                img.Save();
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        public static void DeleteImagesByListID(string listID)
        {
            try
            {
                ProductImages imgs = new ProductImages();
                imgs.Where.ID.Value = listID;
                imgs.Where.ID.Operator = MyGeneration.dOOdads.WhereParameter.Operand.In;
                imgs.Query.Load();
                do
                {
                    imgs.MarkAsDeleted();
                }
                while (imgs.MoveNext());
                imgs.Save();
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        public static void SetDefaultImage(int productID, int imageID)
        {
            try
            {
                ProductImages imgs = new ProductImages();
                imgs.Where.ProductID.Value = productID;
                imgs.Query.Load();

                for (int i = 0; i < imgs.RowCount; i++)
                {
                    imgs.Default = (imgs.ID == imageID);
                    imgs.MoveNext();
                }
                imgs.Save();
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }
    }
}
